package com.zhenhua.dao.impl;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.common.dao.BaseDao;
import com.common.entity.Charge;
import com.sun.org.apache.regexp.internal.recompile;
import com.zhenhua.dao.IChargeDao;

public class ChargeDaoImpl extends BaseDao implements IChargeDao{

	@Override
	public String getNameById(int id) {
		String name=null;
		String sql="select proName from charge where proNumber="+id;
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		try {
			connection=getConnection();
			preparedStatement=connection.prepareStatement(sql);
			resultSet=preparedStatement.executeQuery();
			if (resultSet.next()) {
				name=resultSet.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return name;
	}

	@Override
	public Object getEntity(ResultSet rs) {
		Charge charge=new Charge();
		try {
			
				charge.setProNumber(rs.getInt("proNumber"));
				charge.setAmount(rs.getDouble("amount"));
				charge.setProName(rs.getString("proName"));
				charge.setState(rs.getString("state"));
				charge.setRemarks(rs.getString("remark"));
				charge.setChargeFlag(rs.getInt("flag"));
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return charge;
	}

	@Override
	public List<Charge> getAll(int flag) {
		String sql="select * from charge where flag=?";
		List<Charge> list=extuteQuery(sql,flag);
		return list;
	}

	@Override
	public int getNumByName(String name) {
		String sql="SELECT * FROM charge WHERE proName=?";
		List<Charge> list=extuteQuery(sql,name);
		return list.get(0).getProNumber();
	}

	@Override
	public double getCountById(int id) {
		String sql="SELECT * FROM charge WHERE proNumber=?";
		List<Charge> list=extuteQuery(sql,String.valueOf(id));
		return list.get(0).getAmount();
	}

	@Override
	public List<Charge> listAll() {
		String sql="SELECT * FROM charge where 1=?";
		List<Charge> list=extuteQuery(sql, 1);
		return list;
	}

	@Override
	public int addCharge(Charge charge) {
		String sql="INSERT INTO charge (proName,state,amount,flag,remark) VALUES (?,?,?,?,?)";
		
		Object[] objects=new Object[]{charge.getProName(),charge.getState(),charge.getAmount(),charge.getChargeFlag(),charge.getRemarks()};

		int result=excuteUpdate(sql, objects);
		
		return result;
	}

	@Override
	public int deleteChargeById(int id) {
		String sql="DELETE FROM charge WHERE proNumber=?";
		int result=excuteUpdate(sql, id);
		return result;
	}

	@Override
	public Charge getChargeById(int id) {
		String sql="SELECT * FROM charge WHERE proNumber=?";
		List<Charge> charges=extuteQuery(sql, id);
		
		return charges.get(0);
	}

	@Override
	public int updateChargeById(Charge charge) {
		String sql="UPDATE charge SET proName=?,state=?,amount=?,flag=?,remark=? WHERE proNumber=?";
		Object[] objects=new Object[]{charge.getProName(),charge.getState(),charge.getAmount(),charge.getChargeFlag(),charge.getRemarks(),charge.getProNumber()};
		int result=excuteUpdate(sql, objects);
		return result;
	}

}
